package com.csw.empGrey2.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.csw.empGrey2.entity.Emp;
import com.csw.empGrey2.service.EmpService;

/**
 * @author 1
 */
@Controller
@RequestMapping("emp")
public class EmpController {
	Integer pageSize = 2;
	@Autowired
	private EmpService empService;

	@RequestMapping("add")
	public String addEmpAction(Emp emp) {
		System.out.println("emp//" + emp);
		empService.addEmp(emp);
		return "redirect:/emp/queryAndPage?currPage=1";
	}

	@RequestMapping("delete")
	public String deleteAction(Integer id) {
		empService.deleteAccount(id);
		return "redirect:/emp/queryAndPage?currPage=1";
	}

	@RequestMapping("queryAndPage")
	public String queryAndPage(HttpServletRequest request, Integer currPage) {
		Integer countPage = empService.countPage(pageSize);
		request.setAttribute("countPage", countPage);
		if (currPage == -1) {
			/// 分页
			currPage = 0;/// 分页
		} /// 分页
		if (currPage == 0) {/// 分页
			currPage = 1;/// 分页
		} /// 分页
		if (currPage == countPage + 1) {/// 分页
			currPage = countPage;/// 分页
		} /// 分页
		List<Emp> empList = empService.queryAllAndByPage(currPage, pageSize, countPage);
		request.setAttribute("empList", empList);
		request.setAttribute("currPage", currPage);
		return "emplist";
	}

	@RequestMapping("update")
	public String updateAction(Emp emp, Integer id) {
		emp.setId(id);
		empService.updateBy(emp);
		return "redirect:/emp/queryAndPage?currPage=1";
	}

	@RequestMapping("updateBy")
	public String updateQueryByIdAction(HttpServletRequest request, Integer id) {
		Emp ems = empService.findByCardId(id);
		request.setAttribute("ems", ems);
		return "updateEmp";
	}
}
